//
//  M_SpecialNews.m
//  suRead
//
//  Created by 李 诗洋 on 16/1/20.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "M_SpecialNews.h"

@implementation M_SpecialNews

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"f_id": @"id",
             @"f_title": @"title",
             @"f_banner": @"banner",
             @"f_description": @"description",
             @"f_shareUrl": @"shareUrl",
             @"f_cid": @"cid",
             @"f_catid": @"catid",
             @"f_url": @"url",
             @"f_thumb": @"thumb",
             @"f_inputtime": @"inputtime",
             @"f_views": @"views",
             @"f_commentid":@"commentid",
             @"f_comments": @"comments",
             @"f_commentsUrl":@"commentsUrl"
             };
}

+ (NSValueTransformer *)f_squareJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSNumber *value_, BOOL *success, NSError **error){
        
        if (!value_) {
            return @(0);
        }
        
        return @([value_ integerValue]);
        
    } reverseBlock:^(NSString *id_, BOOL *success, NSError **error) {
        return id_;
    }];
    
}

+ (NSValueTransformer *)f_commentsJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSNumber *value_, BOOL *success, NSError **error){
        
        if (!value_) {
            return @(0);
        }
        
        return @([value_ integerValue]);
        
    } reverseBlock:^(NSString *id_, BOOL *success, NSError **error) {
        return id_;
    }];
    
}

+ (NSValueTransformer *)f_inputtimeJSONTransformer {
    
    return [MTLValueTransformer transformerUsingForwardBlock:^(NSNumber *value_, BOOL *success, NSError **error){
        
        if (!value_) {
            return @" ";
        }
        
        double deltaSeconds = fabs([[NSDate date] timeIntervalSince1970] - value_.doubleValue); // 获取总共秒数
        double deltaMinutes = deltaSeconds / 60.0f;
        
        NSString *dateContent;
        if (deltaSeconds < 5) {
            dateContent = @"刚刚";
        } else if (deltaSeconds < 120) {
            dateContent = @"1分钟前";
        } else if (deltaMinutes < 60) {
            dateContent = [NSString stringWithFormat:@"%i分钟前", (int)deltaMinutes];
        } else if (deltaMinutes < 120) {
            dateContent = @"1小时前";
        } else if (deltaMinutes < (24 * 60)) {
            dateContent = [NSString stringWithFormat:@"%i小时前", (int)floor(deltaMinutes / 60)];
        } else if (deltaMinutes < (24 * 60 * 2)) {
            dateContent = @"昨天";
        } else if (deltaMinutes < (24 * 60 * 7)) {
            dateContent = [NSString stringWithFormat:@"%d天前", (int)floor(deltaMinutes/(60 * 24))];
        } else if (deltaMinutes < (24 * 60 * 14)) {
            dateContent = @"上周";
        } else if (deltaMinutes < (24 * 60 * 31)) {
            dateContent = [NSString stringWithFormat:@"%d周前", (int)floor(deltaMinutes/(60 * 24 * 7))];
        } else if (deltaMinutes < (24 * 60 * 61)) {
            dateContent = @"上个月";
        } else if (deltaMinutes < (24 * 60 * 365.25)) {
            dateContent = [NSString stringWithFormat:@"%d月前", (int)floor(deltaMinutes/(60 * 24 * 30))];
        } else if (deltaMinutes < (24 * 60 * 731)) {
            dateContent = @"去年";
        } else {
            dateContent = [NSString stringWithFormat:@"%i年前", (int)floor(deltaMinutes/(60 * 24 * 731))];
        }
        
        return dateContent;
        
    } reverseBlock:^(NSString *id_, BOOL *success, NSError **error) {
        return id_;
    }];
}


@end
